package com.letter.otools.day.util;


import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

/**
 * Date工具类
 */
public final class DateUtil {

    private static final int TOMORROWDAT = 1;

    public static int calAge(Date birthDate) {
        // 当前日历
        Calendar nowCalendar = Calendar.getInstance();
        // 生日大于当前日期
        if (nowCalendar.before(birthDate)) {
            throw new IllegalArgumentException("The birth date is before current time, it's unbelievable");
        }
        // 当前年月日
        int yearNow = nowCalendar.get(Calendar.YEAR);
        int monthNow = nowCalendar.get(Calendar.MONTH);
        int dayNow = nowCalendar.get(Calendar.DAY_OF_MONTH);
        // 出生日期年月日
        Calendar birthCalendar = Calendar.getInstance();
        birthCalendar.setTime(birthDate);
        int yearBirth = birthCalendar.get(Calendar.YEAR);
        int monthBirth = birthCalendar.get(Calendar.MONTH);
        int dayBirth = birthCalendar.get(Calendar.DAY_OF_MONTH);
        // 粗计算年龄
        int age = yearNow - yearBirth;
        // 当前月份小于出生月份年龄减一
        if (monthNow < monthBirth) {
            age--;
        }
        // 当前月份等于出生月份，日小于生日年龄减一
        else if (monthNow == monthBirth && dayNow < dayBirth) {
            age--;
        }
        // 返回年龄值
        return age;
    }

    /**
     * 判断是否为明天(效率比较高)
     *
     * @return true明天 false不是
     * @throws ParseException
     */
    public static boolean isTomorrow(Date date) {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == TOMORROWDAT) {
                return true;
            }
        }
        return false;
    }

    public static Date millis2Date(final long millis) {
        return new Date(millis);
    }

    public static boolean isSameYear(Date date1, Date date2) {
        if (date1 != null && date2 != null) {
            Calendar cal1;
            (cal1 = Calendar.getInstance()).setTime(date1);
            Calendar cal21;
            (cal21 = Calendar.getInstance()).setTime(date2);
            return isSameYear(cal1, cal21);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    public static boolean isSameYear(Calendar cal1, Calendar cal2) {
        if (cal1 != null && cal2 != null) {
            return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    public static boolean isSameDay(Date cal2, Date date2) {
        if (cal2 != null && date2 != null) {
            Calendar cal1;
            (cal1 = Calendar.getInstance()).setTime(cal2);
            Calendar cal21;
            (cal21 = Calendar.getInstance()).setTime(date2);
            return isSameDay(cal1, cal21);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    public static boolean isSameDay(Calendar cal1, Calendar cal2) {
        if (cal1 != null && cal2 != null) {
            return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    public static Calendar getTomorrow() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 1);
        return calendar;
    }
}
